/**
 * Created by gouyuheng on 2017/2/13.
 */
import * as types from '../types'

const state = {
  loading: false,
  isShowTabbar: true,
  isShowHeader: true,
  needBack: false,
  needRight: false,
  needRight2: false,
  needLeft: false,
  title: '孝行通',
  rightContent: '添加',
  leftContent: '',
  rightCallback: function () {
  },
  leftCallback: function () {
  }
}

const getters = {
  [types.STATE_GET_LOADING] (state) {
    return state.loading
  },
  [types.STATE_GET_SHOWTABBAR] (state) {
    return state.isShowTabbar
  },
  [types.STATE_GET_SHOWHEADER] (state) {
    return state.isShowHeader
  },
  [types.STATE_GET_RIGHTCALLBACK] (state) {
    return state.isShowHeader
  },
  [types.STATE_GET_LEFTCALLBACK] (state) {
    return state.isShowHeader
  },
  [types.STATE_GET_TITLE] (state) {
    return state.title
  }
}

const mutations = {
  [types.STATE_SET_LOADING] (state, loading) {
    if (undefined !== loading && typeof loading) {
      state.loading = loading
    }
  },
  [types.STATE_SET_SHOWTABBAR] (state, isShowTabbar) {
    if (undefined !== isShowTabbar && typeof isShowTabbar) {
      state.isShowTabbar = isShowTabbar
    }
  },
  [types.STATE_SET_SHOWHEADER] (state, isShowHeader) {
    if (undefined !== isShowHeader && typeof isShowHeader === 'boolean') {
      state.isShowHeader = isShowHeader
    }
  },
  [types.STATE_SET_LEFTCALLBACK] (state, leftCallback) {
    if (undefined !== leftCallback && typeof leftCallback === 'function') {
      state.leftCallback = leftCallback
    }
  },
  [types.STATE_SET_RIGHTCALLBACK] (state, rightCallback) {
    if (undefined !== rightCallback && typeof rightCallback === 'function') {
      state.rightCallback = rightCallback
    }
  },
  [types.STATE_SET_TITLE] (state, title) {
    if (undefined !== title && typeof title === 'function') {
      state.title = title
    }
  },
  [types.STATE_SET_STATE] (state, obj) {
    if (obj === undefined) {
      obj = {}
    }
    if (obj.isShowHeader === false) { // 控制是否显示header
      state.isShowHeader = false
    } else {
      state.isShowHeader = true
    }
    if (obj.isShowTabbar === false) { // 控制是否显示tabbar
      state.isShowTabbar = false
    } else {
      state.isShowTabbar = true
    }
    if (obj.title !== undefined) { // 设置header标题
      state.title = obj.title
    } else {
      state.title = '孝行通'
    }
    if (obj.needBack === false) { // 是否显示返回
      state.needBack = false
    } else {
      state.needBack = true
    }
    if (obj.needRight === false) { // 是否显示header右侧按钮
      state.needRight = false
    } else {
      state.needRight = true
    }
    if (obj.needRight2 === true) { // 是否显示header右侧按钮
      state.needRight2 = true
    } else {
      state.needRight2 = false
    }
    if (obj.needLeft === true) { // 是否显示左侧按钮
      state.needLeft = true
    } else {
      state.needLeft = false
    }
    if (obj.leftContent !== undefined) {
      state.leftContent = obj.leftContent
    } else {
      state.leftContent = ''
    }
    if (obj.rightContent !== undefined) {
      state.rightContent = obj.rightContent
    } else {
      state.rightContent = '添加'
    }
    if (undefined !== obj.rightCallback && typeof obj.rightCallback === 'function') {
      state.rightCallback = obj.rightCallback
    } else {
      state.rightCallback = function () {
      }
    }
    if (undefined !== obj.leftCallback && typeof obj.leftCallback === 'function') {
      state.leftCallback = obj.leftCallback
    } else {
      state.leftCallback = function () {
      }
    }
  }
}

export default {
  state,
  getters,
  mutations
}
